package vcp.models;


import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Observable;

import vcp.application.UserManagement;
import vcp.common.AbstractModel;
import client.Message;
import control.SimpleClient;
import db.parking.beans.Orders;
import db.parking.beans.Parking;
import db.parking.beans.ParkingLot;

/**
 * Save parking model
 *
 */
public class SaveParkingModel  extends Observable implements PropertyChangeListener {
	protected SimpleClient client = null;
	protected Message msg = null;
	int[][] floor = null;
	ParkingLot pl = null;
	ParkingLot plarr[];
	int userLot;
	
	/**
	 * save Parking request
	 * @param bean
	 */
	public void saveParking(Orders bean) {
		
		client = SimpleClient.getInstance();
		client.addPropertyChangeListener(this);
		Message msg = new Message(bean, "SavingParking");
		try {
			client.sendToServer(msg);
			
		} catch (Exception ex) {
			System.out.println(ex.toString() + " SavingParkingModel->saveParking");
		}
		
	}

	
	
	/**
	 * get Parking Object request
	 */
	public void getParkingObject() {
		client = SimpleClient.getInstance();
		client.addPropertyChangeListener(this);
		Message msg = new Message(null, "GetParkingObject");
		try {
			client.sendToServer(msg);		
		} catch (Exception ex) {
			System.out.println(ex.toString() + " SavingParkingModel->GetParkingObject");
		}
	}


	/**
	 * Sqitches parking level
	 * @param level
	 */
	public void switchLevel(int level) {		
		if(pl!=null){
			pl.setFlor(level-1);			
			
			setChanged();
			notifyObservers(pl);
		}
	}

	@Override
	public void propertyChange(PropertyChangeEvent event) {
		if (event.getPropertyName().equals("MessageUpdate")) {
		    msg = client.getReturnMsg();
		    if(msg.getMsg().equals("GetParkingObject"))
		    {		    	
		    	userLot = UserManagement.getInstance().getEmployee().getLotNo();
		    	plarr = (ParkingLot[]) msg.getObj();
		    	pl = plarr[userLot-1];
		    	pl.setLotID(userLot);
		    	pl.setFlor(0);
		    	setChanged();
				notifyObservers(pl);	
		    }
		    			
        }
		client.clearPropertyChangeListener();
	}
	
	

	/**
	 * get lots by lot number request
	 * @param userLot
	 */
	public void getLotByLotNum(int userLot) {
		client = SimpleClient.getInstance();
		client.addPropertyChangeListener(this);
		Message msg = new Message(userLot, "GetLotByLotNum");
		try {
			client.sendToServer(msg);		
		} catch (Exception ex) {
			System.out.println(ex.toString() + " SavingParkingModel->GetLotByLotNum");
		}
	}



	public void SavePlState(ArrayList<Object> obj) {
		client = SimpleClient.getInstance();
		client.addPropertyChangeListener(this);
		Message msg = new Message(obj, "SavePlState");
		try {
			client.sendToServer(msg);		
		} catch (Exception ex) {
			System.out.println(ex.toString() + " SavingParkingModel->SavePlState");
		}
	}

	
}
